/**
 * MusicBrowserServlet.java
 */
package com.example.myproject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class MusicBrowserServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		AudioUploadDAO audio = new AudioUploadDAO();
		List<AudioUploadDTO> list = audio.list();
		Map<String, String> result = new HashMap<String, String>();
		StringBuffer resultHtml = new StringBuffer();
			
		/* 
		 * to retrieve music genre from AudioUploadDAO 
		 * it includes html 
		 */
		for(int i=0; i < list.size(); i++)
		{
			if(!"".equals(list.get(i).getGenre()) && list.get(i).getGenre() != null)
			{
				if(i==0)
				{
					resultHtml.append("<li>");
					resultHtml.append("<a href=musicbrowser?fileType=audio&sortType="+list.get(i).getGenre()+">");
					resultHtml.append(list.get(i).getGenre());
					resultHtml.append("</a></li>");
					result.put(list.get(i).getGenre(), list.get(i).getGenre());
				}
				else
				{
					if(!result.containsKey(list.get(i).getGenre()))
					{
						resultHtml.append("<li>");
						resultHtml.append("<a href=musicbrowser?fileType=audio&sortType="+list.get(i).getGenre()+">");
						resultHtml.append(list.get(i).getGenre());
						resultHtml.append("</a></li>");
						result.put(list.get(i).getGenre(), list.get(i).getGenre());
					}
				}
			}
		}
						
		req.setAttribute("musicsort", resultHtml);

		// set jsp page to move
		RequestDispatcher view = req.getRequestDispatcher("index.jsp");
	    view.forward(req, resp);			

	}

}